package com.yang.game;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.yang.game.utils.OkHttpClientManager;

public class RegisterActivity extends AppCompatActivity {

    private Button registerBtn;
    private TextView account;
    private TextView password;
    private TextView verifyPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        registerBtn = findViewById(R.id.registerBtn);
        account = findViewById(R.id.registerAccountText);
        password = findViewById(R.id.registerPasswordText);
        verifyPassword = findViewById(R.id.verifyPasswordText);

        registerBtn.setOnClickListener(v -> {
            Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
            String strAccount = account.getText().toString();
            String strPassword = password.getText().toString();
            String strVerifyPassword = verifyPassword.getText().toString();
            if (strAccount.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "账号为空", Toast.LENGTH_LONG).show();
            } else if (strPassword.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "密码为空", Toast.LENGTH_LONG).show();
            } else if (strVerifyPassword.isEmpty() || !strPassword.equals(strVerifyPassword)) {
                Toast.makeText(RegisterActivity.this, "两次输入密码不一致", Toast.LENGTH_LONG).show();
            } else {
                OkHttpClientManager.Param param1 = new OkHttpClientManager.Param("account", strAccount);
                OkHttpClientManager.Param param2 = new OkHttpClientManager.Param("password", strPassword);
                OkHttpClientManager.Param param3 = new OkHttpClientManager.Param("verifyPassword", strVerifyPassword);
                OkHttpClientManager.postAsyn("http://10.99.212.127:9090/api/user/register", new OkHttpClientManager.ResultCallback<Integer>() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }
                    @Override
                    public void onResponse(Integer response) {
                        if (response == null) {
                            Toast.makeText(RegisterActivity.this, "注册请求错误", Toast.LENGTH_LONG).show();
                            return;
                        }
                        String res = response == 1 ? "注册成功" : response == -1 ? "注册信息缺失" : "该账号已存在";
                        Toast.makeText(RegisterActivity.this, res, Toast.LENGTH_LONG).show();
                        if (response == 1) {
                            startActivity(intent);
                        }
                    }
                }, param1, param2, param3);
            }
        });
    }
}